/*
 *this file is part of nseer erp
 *Copyright (C)2006-2010 Nseer(Beijing) Technology co.LTD/http://www.nseer.com 
 *
 *This program is free software; you can redistribute it and/or
 *modify it under the terms of the GNU General Public License
 *as published by the Free Software Foundation; either
 *version 2 of the License, or (at your option) any later version.
 */
package oa.intrmessage;
 
 
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.sql.*;

import javax.servlet.*;

import java.util.* ;
import java.io.* ;
import include.nseer_db.*;
import include.nseer_cookie.*;

public class check_ok extends HttpServlet{

public synchronized void service(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
HttpSession dbSession=request.getSession();
JspFactory _jspxFactory=JspFactory.getDefaultFactory();
PageContext pageContext = _jspxFactory.getPageContext(this,request,response,"",true,8192,true);
ServletContext dbApplication=dbSession.getServletContext();
int finished_tag=0;
try{

ServletContext application;
HttpSession session=request.getSession();
ServletContext context=session.getServletContext();
String path=context.getRealPath("/");
nseer_db_backup1 oa_db=new nseer_db_backup1(dbApplication);
if(oa_db.conn((String)dbSession.getAttribute("unit_db_name"))){
Note note=new Note();
Email mail=new Email();
String intrmessage_ID=request.getParameter("intrmessage_ID");
String id_value=request.getParameter("id_value");
String id[]=id_value.split("@");
String checker=request.getParameter("checker");
String checker_ID=request.getParameter("checker_id");
String check_time=request.getParameter("check_time");
int check_amount=0;
String content="";
String file_name="";
String subject="";
String tool_type="";
int n=0;
for(int j=0;j<id.length;j++){
String sqls="select * from oa_messager where id='"+id[j]+"' and tool_type='发邮件'";
ResultSet rs1=oa_db.executeQuery(sqls);
if(rs1.next()){
	n++;
}
}
String email_box[]=new String[n];
int p=0;
String sql2="select * from oa_intrmessage where intrmessage_ID='"+intrmessage_ID+"'";
ResultSet rs2=oa_db.executeQuery(sql2);
if(rs2.next()){
	check_amount=rs2.getInt("check_amount")+1;
	subject=rs2.getString("subject");
	content=rs2.getString("content");
	file_name=rs2.getString("attachment1");
	File file=new File(path+"oa/file_attachments/"+file_name);
	
for(int i=1;i<id.length;i++){
	String sql="select * from oa_messager where id='"+id[i]+"'";
	ResultSet rs=oa_db.executeQuery(sql);
	if(rs.next()){
    String tool=rs.getString("tool_type");
    String number=rs.getString("content");
    if(tool.equals("发短信")){
    	tool_type="cell";
    }else if(tool.equals("发邮件")){
    	tool_type="email";
    	email_box[p]=number;
    	p++;
    }
	String sql1="insert into oa_intrmessage_details(intrmessage_ID,type,batch_amount,"+tool_type+") values('"+intrmessage_ID+"','"+tool+"','"+check_amount+"','"+number+"')";
	oa_db.executeUpdate(sql1);
	if(tool.equals("发短信")){
		note.send("bjnseer","8888",number,content);
	}
	}else{
		response.sendRedirect("oa/intrmessage/check_ok.jsp?finished_tag=0");
	}
}
if(email_box.length>0){
mail.send(email_box,"smtp.sina.com.cn","yhuser@sina.com","123456",subject,content);
}	
	

if(id.length==1){	
  	response.sendRedirect("oa/intrmessage/check_ok.jsp?finished_tag=1");
	}else{
	String sql1="update oa_intrmessage set checker='"+checker+"',checker_ID='"+checker_ID+"',check_time='"+check_time+"',check_amount='"+check_amount+"',check_tag='1' where intrmessage_ID='"+intrmessage_ID+"'";
	oa_db.executeUpdate(sql1);
	
   response.sendRedirect("oa/intrmessage/check_ok.jsp?finished_tag=2");
	}

oa_db.commit();
oa_db.close();
}else{
	response.sendRedirect("oa/intrmessage/check_ok.jsp?finished_tag=3");
}
}else{
	response.sendRedirect("error_conn.htm");
}
}catch (Exception ex){
ex.printStackTrace();
}
}
}